package com.example.testrecorder;

import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;

import androidx.annotation.RequiresApi;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.testrecorder.domain.User;
import com.example.testrecorder.utils.BitmapUtil;
import com.example.testrecorder.utils.DateUtil;
import com.example.testrecorder.utils.LogUtil;
import com.example.testrecorder.utils.PermissionUtil;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * @author Yingyong Lao
 * 创建时间 2021/5/23 18:25
 * @version 1.0
 */
public class ViewPerDataFragment extends Fragment {
    private Toolbar toolbar;
    private TextView accountTv,trueNameTv,telTv,emailTv,ageTv,genderTv,registerDateTv;
    public CircleImageView cv;
    private LinearLayout qaaLl;
    private Button editBtn;
    private User currUser;
    public Bitmap bitmapFromPath;
    private static final String TAG = "ViewPerDataFragment";

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_view_per_data, container, false);
        toolbar=v.findViewById(R.id.toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().finish();
            }
        });
        currUser=MyApplication.currUser;
        cv=v.findViewById(R.id.cv);
        String headPortraitStr = currUser.getHeadPortraitStr();
        if(!TextUtils.isEmpty(headPortraitStr)){
            bitmapFromPath = BitmapUtil.getBitmapFromPath(headPortraitStr);
            PermissionUtil.reqWriteAndReadPermission(getActivity(), new PermissionUtil.PermittedCallBack() {
                @Override
                public void permitted() {
                    cv.setImageBitmap(bitmapFromPath);
                }
            });
        }
        accountTv=v.findViewById(R.id.accountTv);
        accountTv.setText(currUser.getUserName());
        trueNameTv=v.findViewById(R.id.trueNameTv);
        trueNameTv.setText(currUser.getTrueName());
        telTv=v.findViewById(R.id.telTv);
        telTv.setText(currUser.getTel());
        emailTv=v.findViewById(R.id.emailTv);
        emailTv.setText(currUser.getEmail());
        ageTv=v.findViewById(R.id.ageTv);
        ageTv.setText(""+currUser.getAge()+"岁");
        genderTv=v.findViewById(R.id.genderTv);
        genderTv.setText(currUser.getGender());
        registerDateTv=v.findViewById(R.id.registerDateTv);
        registerDateTv.setText(DateUtil.getFormatDateStr(currUser.getRegisterDate()));
        qaaLl=v.findViewById(R.id.qaaLl);
        qaaLl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PersonalDataActivity activity = (PersonalDataActivity) getActivity();
                FragmentTransaction fragmentTransaction = activity.fragmentManager.beginTransaction();
                SecurityQuestionFragment fragment=new SecurityQuestionFragment();
                fragmentTransaction.replace(R.id.container,fragment);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
            }
        });
        editBtn=v.findViewById(R.id.editBtn);
        editBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PersonalDataActivity activity = (PersonalDataActivity) getActivity();
                FragmentTransaction fragmentTransaction = activity.fragmentManager.beginTransaction();
                EditPerDataFragment editPerDataFragment = new EditPerDataFragment();
                fragmentTransaction.replace(R.id.container,editPerDataFragment);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
            }
        });
        return v;
    }

    @Override
    public void onDetach() {
        super.onDetach();
        LogUtil.logE(TAG,"活动和碎片解除关联……");
        PersonalDataActivity activity = (PersonalDataActivity) getActivity();
        FragmentManager fragmentManager = activity.fragmentManager;
        int count= fragmentManager.getBackStackEntryCount();
        LogUtil.logD(TAG,""+count);
        if(count==0){
            activity.finish();//如果返回栈中没有碎片了，直接将活动销毁，而不是显示空白的活动
        }
    }
}